package com.zhc.dejingapi.pojo.dingtalk;

import lombok.Data;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Date;

@Data
public class Attendance_Check_Record_Data_Model {
//            "bizId": "EE56A8120767948162830662511989ED",
    private String bizId;
//            "corpId": "ding34c75b368965adeb35c2f4657eb6378f",
    private String corpId;
//            "userId": "01215957435526304549",
    private String userId;
    private String userName;
    private String userCode;
    private String deptId;
    private String deptName;
//            "address": "大门出通道",
    private String address;
//            "groupId": "25382C3EE3F843957A335D807E2B8D6E",
    private String groupId;
//            "deviceSN": "940519230531KN1393",
    private String deviceSN;
//            "checkTime": 1724798876000,
    private Long checkTime;
    private Date checkDate;
//            "deviceName": "大门出通道",
    private String deviceName;
//            "checkByUser": false,
    private boolean checkByUser;
//            "locationMethod": "ATM",
    private String location;
//            "locationResult": "Normal"
    private String locationResult;

    public void CalCheckDate(){
        this.checkDate = new Date(this.checkTime);
    }

    public void GetUserDeptInfo(JdbcTemplate qingflowDBTemplate) throws Exception{

        String strSql="select count(*)  \n"  +
                "from ctct_member\n" +
                "where ex_user_id=?";
        int iHaveCount=qingflowDBTemplate.queryForObject(strSql,Integer.class,this.userId);
        if(iHaveCount<1){
            return;
        }
        //id,remark
         strSql="SELECT id\n" +
                "from ctct_member\n" +
                "where ex_user_id=?";

        String strUid=qingflowDBTemplate.queryForObject(strSql,String.class,this.userId);
        if(strUid==null||strUid.isEmpty()){
           return;
        }
        strSql="select remark\n" +
                "from ctct_member\n" +
                "where id=?";

        userCode=strUid;
        userName=qingflowDBTemplate.queryForObject(strSql,String.class,strUid);

        strSql="SELECT count(*) from ctct_dept_rel where member_id=?";
        iHaveCount=qingflowDBTemplate.queryForObject(strSql,Integer.class,strUid);
        if(iHaveCount<1){
            return;
        }
        strSql="SELECT dept_id from ctct_dept_rel where member_id=?  LIMIT 1";

        try{
            deptId=qingflowDBTemplate.queryForObject(strSql,String.class,strUid);
            strSql="SELECT dept_name from ctct_dept WHERE id=?";
            deptName=qingflowDBTemplate.queryForObject(strSql,String.class,deptId);
        }
        catch (Exception e){
            e.printStackTrace();
            System.out.println(strSql);
            System.out.println(strUid);
            System.out.println(deptId);
            throw e;
        }



    }

    public void SaveToDB(JdbcTemplate dingDBTemplate){
        String strSql="Insert into ding_usercheckrecord(\n" +
                "bizId,corpId,userId,userCode,\n" +
                "userName,deptId,deptName,address,\n" +
                "groupId,deviceSN,checkTime,checkDate,\n" +
                "deviceName,checkByUser,location,locationResult ) \n"+
                " values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ";

        dingDBTemplate.update(strSql,
                this.bizId,this.corpId,this.userId,this.userCode,
                this.userName,this.deptId,this.deptName,this.address,
                this.groupId,this.deviceSN,this.checkTime,this.checkDate,
                this.deviceName,this.checkByUser,this.location,this.locationResult);
    }
}
